package edu.avans.hartigehap.web.controller;

import java.util.Collection;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.avans.hartigehap.domain.WebDiscountInterface;
import edu.avans.hartigehap.domain.WebDish;
import edu.avans.hartigehap.domain.WebMenuItem;
import edu.avans.hartigehap.domain.WebOrder;
import edu.avans.hartigehap.service.DiningTableService;
import edu.avans.hartigehap.service.RestaurantService;
import edu.avans.hartigehap.service.WebDiscountService;
import edu.avans.hartigehap.service.WebDishService;
import edu.avans.hartigehap.service.WebMailService;
import edu.avans.hartigehap.service.WebOrderService;
import edu.avans.hartigehap.service.jpa.WebMailServiceImpl;
import edu.avans.hartigehap.web.form.Message;

@RequestMapping("/")
@Controller
public class WebOrderController {	

	final Logger logger = LoggerFactory.getLogger(WebOrderController.class);

	@Autowired
	private MessageSource messageSource;
	@Autowired
	private RestaurantService restaurantService;
	@Autowired
	private DiningTableService diningTableService;
	@Autowired
	private WebOrderService webOrderService;
	@Autowired
	private WebDishService webDishService;
	@Autowired
	private WebDiscountService webDiscountService;


	@RequestMapping(value = { "/pizzas" }, method = RequestMethod.GET)
	public String listPizzas(Model uiModel) {

		WebOrder webOrder = createWebOrder();
		uiModel.addAttribute("webOrder", webOrder);

		Collection<WebMenuItem> dishes = webDishService.findAllPizzas();
		uiModel.addAttribute("dishes", dishes);

		return "hartigehap/pizzas";
	}


	@RequestMapping(value = {"/weborders/{orderId}/pizzas/{dishId}/ingredients"}, method = RequestMethod.GET)
	public String listToppings(@PathVariable("orderId") String orderId, @PathVariable("dishId") String dishId, Model uiModel) {

		WebOrder webOrder = webOrderService.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("webOrder", webOrder);

		Collection<WebMenuItem> toppings = webDishService.findAllToppings();
		uiModel.addAttribute("toppings", toppings);

		WebDish webDish = webDishService.findById(Long.valueOf(dishId));
		uiModel.addAttribute("dish", webDish);

		uiModel.addAttribute("id", webDish.getId());

		return "hartigehap/toppings";
	}

	@RequestMapping(value = {"/weborder/{orderId}/pizzas"}, method = RequestMethod.GET)
	public String listPizzasFromToppings(@PathVariable("orderId") String orderId, Model uiModel) {

		Collection<WebMenuItem> dishes = webDishService.findAllPizzas();
		uiModel.addAttribute("dishes",dishes);

		WebOrder webOrder = webOrderService.fetchWarmedUp(Long.valueOf(orderId));

		uiModel.addAttribute("webOrder", webOrder);

		return "hartigehap/pizzas";
	}



	@RequestMapping(value = {"/weborders/{orderId}/pizzas/{dishId}/ingredients"}, method = RequestMethod.POST)
	public String addTopping(@PathVariable("orderId") String orderId, @PathVariable("dishId") String dishId, 
			@RequestParam String toppingName, Model uiModel) {

		WebOrder webOrder = webOrderService
				.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("webOrder", webOrder);

		Collection<WebMenuItem> toppings = webDishService.findAllToppings();
		uiModel.addAttribute("toppings", toppings);

		WebDish webDish = webDishService.findById(Long.valueOf(dishId));
		uiModel.addAttribute("dish", webDish);

		Long id = webOrderService.decoratePizza(webOrder, webDish, toppingName);
		uiModel.addAttribute("id", id);

		return "hartigehap/toppings";
	}

	@RequestMapping(value = "/weborders/{orderId}", method = RequestMethod.GET)
	public String showOrder(@PathVariable("orderId") String orderId,
			Model uiModel) {

		WebOrder webOrder = webOrderService
				.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("webOrder", webOrder);

		return "hartigehap/weborder";
	}

	@RequestMapping(value = "/weborders/{orderId}", method = RequestMethod.POST)
	public String submitOrder(@PathVariable("orderId") String orderId,
			Model uiModel, Locale locale, String email) {
		
		if (!email.equals("")) {
			WebMailService mailService = new WebMailServiceImpl();
			mailService.sendMail(email);

			uiModel.addAttribute(
					"message",
					new Message("success", messageSource
							.getMessage("message_bestelling_verzonden",
									new Object[] {}, locale)));
		} else {
			uiModel.addAttribute(
					"message",
					new Message("error", messageSource
							.getMessage("message_email_ontbreekt",
									new Object[] {}, locale)));
		}

		WebOrder webOrder = webOrderService
				.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("webOrder", webOrder);



		return "hartigehap/weborder";
	}

	@RequestMapping(value = "/orders/{orderId}/pizzas", method = RequestMethod.POST)
	public String addWebOrderItem(@PathVariable("orderId") String orderId,
			@RequestParam String dishName,
			RedirectAttributes redirectAttributes, Model uiModel, Locale locale) {

		logger.info("orderId = " + orderId);
		logger.info("dishName = " + dishName);

		WebOrder webOrder = webOrderService
				.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("webOrder", webOrder);

		Collection<WebMenuItem> dishes = webDishService.findAllPizzas();
		uiModel.addAttribute("dishes", dishes);

		Long id = webOrderService.addOrderItem(webOrder, dishName);

		uiModel.addAttribute(
				"message",
				new Message("success", messageSource.getMessage(
						"message_dish_add", new Object[] {}, locale)));

		redirectAttributes.addFlashAttribute("dishname", dishName);
		return "redirect:/weborders/" + orderId + "/pizzas/" + id + "/ingredients";

	}



	@RequestMapping(value = "/orders/{orderId}/pizzas/{pizzaName}", method = RequestMethod.DELETE)
	public String deleteOrderItem(@PathVariable("orderId") String orderId,
			@PathVariable("pizzaName") String pizzaName, Model uiModel) {

		logger.info("orderId = " + orderId);
		logger.info("pizzaName = " + pizzaName);

		WebOrder webOrder = webOrderService
				.fetchWarmedUp(Long.valueOf(orderId));
		uiModel.addAttribute("diningTable", webOrder);

		webOrderService.deleteOrderItem(webOrder, pizzaName);

		return "redirect:/diningTables/" + orderId;
	}

	public WebOrder createWebOrder() {
		WebOrder order = webOrderService.createNewWebOrder();
		WebDiscountInterface discount = webDiscountService
				.createNewWebDiscount();
		webOrderService.setOrderDiscount(order, discount);
		return order;
	}

	public void cannotBeAlteredMessage(@RequestParam Model uiModel, Locale locale) {
		uiModel.addAttribute("message", new Message("error", 
				messageSource.getMessage("message_cannot_be_altered", new Object[]{}, locale))); 
	}


}
